package com.hkts.naturalgas.workovertime.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.organizational.entity.HontPost;
import com.hkts.naturalgas.organizational.entity.HontUser;
import com.hkts.naturalgas.organizational.mapper.HontPostMapper;
import com.hkts.naturalgas.organizational.mapper.HontUserMapper;
import com.hkts.naturalgas.organizational.mapper.HontUserPostMapper;
import com.hkts.naturalgas.performance.performanceVo.HontPostTitleVo;
import com.hkts.naturalgas.workovertime.entity.HkOvertimeUser;
import com.hkts.naturalgas.workovertime.mapper.HkOvertimeUserMapper;
import com.hkts.naturalgas.workovertime.service.IHkOvertimeUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hml
 * @since 2023-08-31
 */
@Service
@Transactional
public class HkOvertimeUserServiceImpl extends ServiceImpl<HkOvertimeUserMapper, HkOvertimeUser> implements IHkOvertimeUserService {


    @Autowired
    private HkOvertimeUserMapper hkOvertimeUserMapper;
    @Autowired
    private HontUserPostMapper hontUserPostMapper;
    @Autowired
    private HontUserMapper hontUserMapper;

    @Override
    public IPage<HkOvertimeUser> selectPage(Page<HkOvertimeUser> page, HkOvertimeUser hkOvertimeUser) {
        QueryWrapper<HkOvertimeUser> query = Wrappers.query();
        QueryWrapper<HkOvertimeUser> entityWrapper = getEntityWrapper(query, hkOvertimeUser);
        return hkOvertimeUserMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HkOvertimeUser> selectList(HkOvertimeUser hkOvertimeUser) {
        QueryWrapper<HkOvertimeUser> query = Wrappers.query();
        QueryWrapper<HkOvertimeUser> entityWrapper = getEntityWrapper(query, hkOvertimeUser);
        return hkOvertimeUserMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HkOvertimeUser> getEntityWrapper(QueryWrapper<HkOvertimeUser> entityWrapper, HkOvertimeUser hkOvertimeUser) {
        //条件拼接
        if (StringUtils.isNotBlank(hkOvertimeUser.getHontUserId())) {
            entityWrapper.like(HkOvertimeUser.HONT_USER_ID, hkOvertimeUser.getHontUserId());
        }
        if (StringUtils.isNotBlank(hkOvertimeUser.getNickName())) {
            entityWrapper.like(HkOvertimeUser.NICK_NAME, hkOvertimeUser.getNickName());
        }
        if (StringUtils.isNotBlank(hkOvertimeUser.getDepartmentId())) {
            entityWrapper.like(HkOvertimeUser.DEPARTMENT_ID, hkOvertimeUser.getDepartmentId());
        }
        if (StringUtils.isNotBlank(hkOvertimeUser.getDepartmentTitle())) {
            entityWrapper.like(HkOvertimeUser.DEPARTMENT_TITLE, hkOvertimeUser.getDepartmentTitle());
        }
        if (StringUtils.isNotBlank(hkOvertimeUser.getHkOvertimeId())) {
            entityWrapper.like(HkOvertimeUser.HK_OVERTIME_ID, hkOvertimeUser.getHkOvertimeId());
        }
        return entityWrapper;
    }

    @Override
    public List<String> addHkOvertimeUsers(List<HkOvertimeUser> hkOvertimeUsers, String hkOvertimeId, LocalDateTime createTime) throws Exception {
        if (hkOvertimeUsers.size() == 0) {
            return new ArrayList<>();
        }

        List<String> userIds = new ArrayList<>();
        // TODO 检测用户是否有岗位，循环查数据库需要优化

        for (int i = 0; i < hkOvertimeUsers.size(); i++) {
            userIds.add(hkOvertimeUsers.get(i).getHontUserId());
        }

        List<HontPostTitleVo> byUserIds = hontUserPostMapper.getByUserIds(userIds);
        List<String> noPostUserIds = new ArrayList<>();
        //过滤没有岗位的用户
        hkOvertimeUsers.forEach(t -> {
            String userId = t.getHontUserId();
            List<HontPostTitleVo> collect = byUserIds.stream()
                    .filter(hontPostTitleVo -> hontPostTitleVo.getHontUserId().equals(userId))
                    .collect(Collectors.toList());
            if (collect.size() == 0) {
                noPostUserIds.add(userId);
            }
        });
        if(noPostUserIds.size()>0){
            QueryWrapper<HkOvertimeUser> query = Wrappers.query();
            QueryWrapper<HontUser> queryHontUser = Wrappers.query();
            queryHontUser.lambda().in(HontUser::getId,noPostUserIds);
            List<HontUser> hontUsers = hontUserMapper.selectList(queryHontUser);
            StringBuffer nameBuffer=new StringBuffer();
            for (int i = 0; i <hontUsers.size() ; i++) {
                nameBuffer.append(hontUsers.get(i).getNickName()).append(",");
            }
            nameBuffer.delete(nameBuffer.length()-1,nameBuffer.length());
            nameBuffer.append("没有配置岗位信息，请先配置岗位后再操作");
            throw new Exception(nameBuffer.toString());

        }else{
            List<String> userIdList = new ArrayList<>();
            hkOvertimeUsers.forEach(t -> {
                t.setHkOvertimeId(hkOvertimeId);
                t.setCreateTime(createTime);
                userIdList.add(t.getHontUserId());
                //
                String userId = t.getHontUserId();
                List<HontPostTitleVo> collect = byUserIds.stream()
                        .filter(hontPostTitleVo -> hontPostTitleVo.getHontUserId().equals(userId))
                        .collect(Collectors.toList());
                t.setPostId(collect.get(0).getId());
                t.setPostTitle(collect.get(0).getTitle());
            });
            hkOvertimeUserMapper.insertBatch(hkOvertimeUsers);
            return userIdList;
        }
    }
}
